using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using ComputerShopE3.Models;
using System.Data.Linq;
namespace ComputerShopE3.Controllers
{
    public class TinInfo
    {
        public int ID;
        public String TieuDe;
        public DateTime NgayDang;
        public String NoiDung;
        public String Ten;
    };
    public class NewsController : Controller
    {
        //
        // GET: /News/
        ComputerShopDBEntities1 _db = new ComputerShopDBEntities1();
        
        public ActionResult Index()
        {
            SiteRoles roles =  SpiderE3AuthorizeAttribute.GetCurrentRole(HttpContext);
            var Tins = 
                from tt in _db.TIN_TUC
                join nd in _db.NGUOI_DUNG on tt.NGUOI_DUNG.id equals nd.id

            select new TinInfo{ ID = tt.id, TieuDe = tt.TieuDe, NgayDang = tt.NgayDang, NoiDung = tt.NoiDung, Ten = nd.Ten };
            return View(Tins.ToList());
        }
        


        //
        // GET: /News/Details/5

        public ActionResult Details(int id)
        {
             return View();
        }

        //
        // GET: /News/Create
        [SpiderE3Authorize(Roles = SiteRoles.Admin | SiteRoles.Employee)]
        public ActionResult Create()
        {
            var nguoidungs = from t in _db.NGUOI_DUNG
                             select t;
            int n = nguoidungs.Count();
            ViewData["NGUOI_DUNG"] = new SelectList(nguoidungs.ToList(), "id", "Ten");
  
            return View();
        }

        [SpiderE3Authorize(Roles = SiteRoles.Admin | SiteRoles.Employee)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(FormCollection form)
        {
           
            try
            {
                string id = form.Get("id");
                string TieuDe = form.Get("TieuDe");
                string NoiDung = form.Get("NoiDung");
                DateTime NgayDang = DateTime.Parse(form.Get("NgayDang"));
                int User = int.Parse(form.Get("IdNguoiDung"));
                TIN_TUC tt = new TIN_TUC
                {
                    TieuDe = TieuDe,
                    NoiDung = NoiDung,
                    NgayDang = NgayDang,
                };
                var t = from nd in _db.NGUOI_DUNG
                        where nd.id == User
                        select nd;
                tt.NGUOI_DUNG = t.First();

                _db.AddToTIN_TUC(tt);
                _db.SaveChanges();

                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                return View(ex.Message);
                // return View();
            }
        }

        //
        // GET: /News/Edit/5
        [SpiderE3Authorize(Roles = SiteRoles.Admin | SiteRoles.Employee)]
        public ActionResult Edit(int id)
        {
            var news = (from m in _db.TIN_TUC

                        where m.id == id

                        select m).First();

            return View(news);
        }
        //
        // POST: /News/Edit/5
        //[Authorize(Roles = "admin")]
        [SpiderE3Authorize(Roles = SiteRoles.Admin | SiteRoles.Employee)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit([Bind()]TIN_TUC tinTucToEdit)
        {
            var originalTinTuc = (from m in _db.TIN_TUC
                                  where m.id == tinTucToEdit.id                                  
                                  select m).First();

            if (!ModelState.IsValid)
                return View(originalTinTuc);

            _db.ApplyPropertyChanges(originalTinTuc.EntityKey.EntitySetName, tinTucToEdit);
            _db.SaveChanges();

            return RedirectToAction("Index");
        }

    }
}
